package easy;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/26 18:04
 */
public class LongestCommonPrefix {
    public static void main(String[] args) {
        String[] strings = {"flower", "flow", "flight"};
        Solution solution = new LongestCommonPrefix().new Solution();
        System.out.println(solution.longestCommonPrefix(strings));
    }

    private class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs == null || strs.length == 0) {
                return "";
            }
            int index = 0;
            boolean notStop = true;
            StringBuilder builder = new StringBuilder();
            while (notStop) {
                if (index > strs[0].length() - 1) {
                    notStop = false;
                    break;
                }
                char std = strs[0].charAt(index);

                for (int i = 1; i < strs.length; i++) {
                    if (index > strs[i].length() - 1) {
                        notStop = false;
                        break;
                    }
                    if (std != strs[i].charAt(index)) {
                        notStop = false;
                        break;
                    }
                }

                if (notStop) {
                    builder.append(std);
                    index += 1;
                }

            }

            return builder.toString();
        }
    }
}
